package health.lm.com.home.presenter;

import java.util.ArrayList;
import java.util.List;

import health.lm.com.component_base.base.mvp.contract.XPresent;
import health.lm.com.home.entity.HomeEntity;
import health.lm.com.home.entity.HomeMultiEntity;
import health.lm.com.home.entity.HomePageListEntity;
import health.lm.com.home.entity.UnreadNotice;
import health.lm.com.home.fragment.HomeFragment;
import health.lm.com.home.model.HomeModel;
import health.lm.com.network.callback.SimpleCallBack;

/**
 * 首页
 *
 * @author GQI
 */
public class HomePresenter extends XPresent<HomeFragment> {
    public void getData(Integer status) {
        HomeModel.getInstance().getHomeData(new SimpleCallBack<HomeEntity>() {
            @Override
            public void onSuccess(HomeEntity pEntity) {
                if (hasV()) {
                    getV().setData(getResource(pEntity));
                    if (status == 1)getV().setTitleData(pEntity.getTitles());
                    getV().setNoticeNum(pEntity.getUnread_num());
                }
            }
        });
//        String json = JsonUtil.getJson(RxTool.getContext(), "home_home.json");
//        HomeEntity homeEntity = new GsonBuilder().create().fromJson(json, new TypeToken<HomeEntity>() {
//        }.getType());
//
//        if (hasV()) {
//            getV().setData(getResource(homeEntity));
//            getV().setTitleData(homeEntity.getTitles());
//        }
    }

    public void notice() {
        HomeModel.getInstance().noticeUnreadNum(new SimpleCallBack<UnreadNotice>() {
            @Override
            public void onSuccess(UnreadNotice unreadNotice) {
                if (hasV()) {
                    //getV().setNoticeNum(unreadNotice);
                }
            }
        });
    }

    public void getData(String id, int page, int pageSize) {
        HomeModel.getInstance().getHomeNewsData(id, page, pageSize, new SimpleCallBack<HomePageListEntity>() {
            @Override
            public void onSuccess(HomePageListEntity pHomePageListEntity) {
                if (hasV()) getV().setNewData(pHomePageListEntity.getNews_list());
            }
        });
        //List<HomePageEntity> entities = new ArrayList<>();
        //for (int a = 0; a < 15; a++) {
        //    HomePageEntity homePageEntity = new HomePageEntity();
        //    homePageEntity.setClick("6754次阅读");
        //    homePageEntity.setPic("http://pic32.nipic.com/20130813/3347542_160503703000_2.jpg");
        //    homePageEntity.setTitle("第五届中国（临沂）国际百货博览会盛大开幕！" + a);
        //    homePageEntity.setType("智库");
        //    entities.add(homePageEntity);
        //}
        //if (hasV()) getV().setNewData(entities);
    }

    private List<HomeMultiEntity> getResource(HomeEntity pEntity) {
        List<HomeMultiEntity> multiEntities = new ArrayList<>();
        HomeMultiEntity multiEntity1 = new HomeMultiEntity(HomeMultiEntity.TYPE_BANNER);
        multiEntity1.setBanner(pEntity.getBanner());
        multiEntities.add(multiEntity1);

        HomeMultiEntity multiEntity2 = new HomeMultiEntity(HomeMultiEntity.TYPE_NOTICE);
        multiEntity2.setNotices(pEntity.getNotice());
        multiEntities.add(multiEntity2);

        /*"is_bussiness":"1（是否有商会）"*/
        if (1 == pEntity.getBusiness().getIs_bussiness()) {
            HomeMultiEntity multiEntity3 = new HomeMultiEntity(HomeMultiEntity.TYPE_BUSINESS);
            multiEntity3.setBusiness(pEntity.getBusiness());
            multiEntities.add(multiEntity3);
        }

        HomeMultiEntity multiEntity4 = new HomeMultiEntity(HomeMultiEntity.TYPE_RECOMMEND);
        multiEntity4.setRecommend(pEntity.getRecommend());
        multiEntities.add(multiEntity4);

        //HomeMultiEntity multiEntity5 = new HomeMultiEntity();
        //multiEntity5.setTitles(pEntity.getTitles());
        //multiEntities.add(multiEntity5);

        return multiEntities;
    }
}
